package com.stu.wgt.test.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.wgt.test.bean.Merchant;
import com.stu.wgt.test.mapper.MerchantMapper;
import com.stu.wgt.test.service.MerchantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wgt
 * @since 2024-02-05
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Autowired
    MerchantMapper merchantMapper;

    @Override
    public int insert(Merchant merchant) {
        QueryWrapper<Merchant> qw = new QueryWrapper<>();
        qw.eq("name", merchant.getName());
        List<Merchant> merchantsList = merchantMapper.selectList(qw);
        if (!merchantsList.isEmpty()) {
            return 0;
        }
        return merchantMapper.insert(merchant);
    }

    @Override
    public int delete(int id) {
        return merchantMapper.deleteById(id);
    }

    @Override
    public int update(Merchant merchant) {
        return merchantMapper.updateById(merchant);
    }

    @Override
    public List<Merchant> selectAllMerchant() {
        return merchantMapper.selectList(null);
    }

    @Override
    public List<Merchant> selectById(int id) {
        QueryWrapper<Merchant> qw = new QueryWrapper<>();
        qw.eq("id", id);
        return merchantMapper.selectList(qw);
    }

    @Override
    public List<Merchant> selectByName(String name) {
        QueryWrapper<Merchant> qw = new QueryWrapper<>();
        qw.eq("name", name);
        return merchantMapper.selectList(qw);
    }

    @Override
    public List<Merchant> selectByTelephone(String telePhone) {
        QueryWrapper<Merchant> qw = new QueryWrapper<>();
        qw.eq("phone", telePhone);
        return merchantMapper.selectList(qw);
    }

    @Override
    public IPage<Merchant> searchMerchant(String query, int pageNum, int pageSize) {
        Page<Merchant> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Merchant> qw = new QueryWrapper<>();
        qw.like("name", query);
        return merchantMapper.selectPage(page, qw);
    }
}
